package com.wholeintech.dao;

import android.content.Context;
import android.util.Log;

import com.wholeintech.entity.VoiceConfig;
import com.wholeintech.zigbee.gw.DeviceManager;
import com.wholeintech.utils.DatabaseUtils;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * 场景数据操作类
 *
 * @author Administrator
 */
public class VoiceConfigDao {
    private final static String TAG = "BoxInfoDao";
    private Context mContext = null;
    public final static String VOICE_TEXT_LIGHT = "开关灯";
    public final static int ENABLE = 1;
    public final static int DISABLE = 0;

    public VoiceConfigDao(Context context) {
        this.mContext = context;
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    public boolean add(VoiceConfig voiceConfig) {
        boolean bRet = false;
        DbManager db = DatabaseUtils.DeviceDbUtils();
        try {
            db.save(voiceConfig);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    /**
     * 初始化灯设备列表
     *
     * @return
     */
    public boolean initLightData() {
        boolean ret = false;
        //DbManager db = DatabaseUtils.DeviceDbUtils();
        DbManager db = DatabaseUtils.DeviceDbUtils();
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO VoiceConfig (voiceText,devId,devName,type,enable) select '" + VOICE_TEXT_LIGHT);
        sql.append("',devId,name,type,1 from DeviceDataBase Where type = " + DeviceManager.TYPE_LIGHT);
        sql.append(" or type = " + DeviceManager.TYPE_LIGHT_DIMMER + "");

        try {
//            db.createTableIfNotExist(VoiceConfig.class); //创建一个表User
            VoiceConfig tableEntity = db.findFirst(VoiceConfig.class);
            try {
                //db.save(tableEntity);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            db.execNonQuery(sql.toString());
            Log.d(TAG, "initLightData:" + sql.toString());
            ret = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return ret;
    }

    /**
     * 删除所有的设备
     */
    public void deleteAll() {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.delete(VoiceConfig.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 删除指定的设备
     *
     * @param strDevId
     */
    public void deleteByDevId(String strDevId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.delete(VoiceConfig.class, WhereBuilder.b("devId", "=", strDevId));
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 修改状态
     *
     * @param strDevId
     * @param enable
     */
    public void setEnable(String strDevId, int enable) {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        VoiceConfig voiceConfig = null;

        /*try {
            voiceConfig = db.selector(VoiceConfig.class).where("devId", "=", strDevId).findFirst();
            if (voiceConfig != null) {
                voiceConfig.setEnable(enable);
                db.saveOrUpdate(voiceConfig);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }*/
    }

    /**
     * @return
     */
    public int getVoiceConfigListCount() {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<VoiceConfig> list = null;

        try {
            list = db.findAll(VoiceConfig.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return list == null ? 0 : list.size();
    }

    /**
     * @param intType
     * @return
     */
    public List<VoiceConfig> getVoiceConfigListByType(int intType) {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<VoiceConfig> list = null;
/*
        try {
            //list = db.findAll(Selector.from(VoiceConfig.class).where("type","=",intType));
           // list = db.selector(VoiceConfig.class).where("type", "=", intType).findAll();
            
            //list =db.findFirst(Selector.from(BoxInfo.class).where("boxId", "=", strBoxId)); 
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
*/
        return list;
    }

    /**
     * @param voiceText
     * @return
     */
 /*   public List<VoiceConfig> getVoiceConfigListByVoiceText(String voiceText) {
    	DbUtils db = DatabaseUtils.DeviceDbUtils(mContext);
        List<VoiceConfig> list = null;

        try {
//            list = db.findAll(Selector.from(VoiceConfig.class).where("voiceText", "=", voiceText));
            list = db.selector(VoiceConfig.class).where("voiceText", "=", voiceText).findAll();

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return list;
    }*/

    /**
     * 获取所有灯设备
     *
     * @return
     */
    /*public List<DeviceDb> getLightDeviceList() {
        List<DeviceDb> deviceList = null;
        DeviceDb device = null;

        Cursor cursor = null;
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<Navigation> list = null;
        StringBuilder sb = new StringBuilder();

        sb.append("select b.* from VoiceConfig a left join DeviceDataBase b on a.devId = b.devId where a.enable=1");

        try {
            int intDevIdIndex;
            int intNameIndex;
            int intTypeIndex;
            int intMacAddrIndex;
            int intBoxIdIndex;

            cursor = db.execQuery(sb.toString());
            if (cursor != null) {
                deviceList = new ArrayList<DeviceDb>();
                while (cursor.moveToNext()) {
                    device = new DeviceDb();
                    intDevIdIndex = cursor.getColumnIndex("devId");
                    intNameIndex = cursor.getColumnIndex("name");
                    intTypeIndex = cursor.getColumnIndex("type");
                    intMacAddrIndex = cursor.getColumnIndex("macAddr");
                    intBoxIdIndex = cursor.getColumnIndex("boxId");
                    //intMacAddrIndex = cursor.getColumnIndex("regionId");
                    //intMacAddrIndex = cursor.getColumnIndex("localRegionId");                                        
                    device.setName(cursor.getString(intNameIndex));
                    device.setDevId(cursor.getString(intDevIdIndex));
                    device.setType(cursor.getInt(intTypeIndex));
                    device.setMacAddr(cursor.getString(intMacAddrIndex));
                    device.setBoxId(cursor.getString(intBoxIdIndex));
                    deviceList.add(device);
                }
            }

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        return deviceList;
    }*/
}
